<template>
  <div :class="$style.wrap">
    <Button type="primary" @click="showHandle">更换质检人</Button>
    <el-dialog :visible.sync="visible" title="更换质检人" :close-on-click-modal="false" @close="resetForm">
      <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item prop="executorBusinessGroupNo" label="质检人业务组">
          <SearchSelectPicker
            url="/call-center/api/v1/businessGroup/page"
            placeholder="请选择质检业务组"
            lable-name="groupName"
            value-name="groupNo"
            :value.sync="formData.executorBusinessGroupNo"
            :lable.sync="formData.executorBusinessGroupName"
            multiple
            collapse-tags
            @selectChange="selectChangeHandle"
          ></SearchSelectPicker>
        </el-form-item>
        <el-form-item prop="executor" label="质检人">
          <SearchSelectPicker
            url="/call-center/api/v1/staffInfo/page"
            :other-raw="{groupNos:formData.executorBusinessGroupNo}"
            placeholder="请选择质检人"
            value-name="accountNo"
            lable-name="staffName"
            :parent-id="formData.executorBusinessGroupNo"
            :value.sync="formData.executor"
            :lable.sync="formData.executorName"
          ></SearchSelectPicker>
        </el-form-item>
        <el-form-item>
          <Button type="primary" @click="confirmHandle">确定</Button>
          <Button type="primary" @click="resetForm">重置</Button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import SearchSelectPicker from '@/components/SearchSelectPicker'
import { qualityCheckReplace } from '@/api/inspection'
import { deepCopy } from '@/utils'
export default {
  name: 'ChangeInspectionPerson',
  components: {
    SearchSelectPicker
  },
  props: {
    selectionList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      formData: {
        executorBusinessGroupNo: [],
        executorBusinessGroupName: [],
        executor: '',
        executorName: ''
      },
      formRules: {
        executor: [{ required: true, message: '请选择质检人', trigger: 'change' }]
      }
    }
  },
  methods: {
    showHandle() {
      this.$checkSelected(this.selectionList).then(() => {
        this.visible = true
      })
    },
    selectChangeHandle() {
      this.$set(this.formData, 'executor', '')
      this.$set(this.formData, 'executorName', '')
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = []
        for (const item of this.selectionList) {
          const v = deepCopy(item)
          v.executor = this.formData.executor
          v.executorName = this.formData.executorName
          data.push(v)
        }
        return qualityCheckReplace(data).then(res => {
          this.$message.success('更换质检人成功！')
          this.$emit('on-refresh')
          this.visible = false
        })
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" module>
.wrap{
  display: inline-block;
}
</style>
